//
//  BankCardManageViewController.m
//  FactorySale
//
//  Created by 主事丫环 on 2020/1/8.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "BankCardManageViewController.h"
#import "BankCardManageTableViewCell.h"
#import "AddBankCardViewController.h"
#import "BankCardManageModel.h"

#import "CommonPopViewController.h"

@interface BankCardManageViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) NSMutableArray *mydateSource;
@property (nonatomic, strong) UIView *tabFootView;
@property (nonatomic, strong) UILabel *timelab;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *addreddLab;
@property (nonatomic, strong) UILabel *phoneLab;
@end

@implementation BankCardManageViewController
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    [self getBankNameListData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"银行卡管理";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    
    [self createOrderDetailUI];
    
    
    
}

- (void)createOrderDetailUI {
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 56)];
    
    _mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight +10, KWIDTH, KHEIGHT-kNaviHeight-2) style:UITableViewStyleGrouped];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    [_mytableView registerClass:[BankCardManageTableViewCell class] forCellReuseIdentifier:@"BankCardManageTableViewCell"];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    _mytableView.tableFooterView = self.tabFootView;
    [self.view addSubview:_mytableView];
    
    [self showtabFootView];
}

-(void)showtabFootView {
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 117)];
    
    UIButton *addBankBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [addBankBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    addBankBut.backgroundColor = [UIColor whiteColor];
    addBankBut.frame = CGRectMake(20, 10, KWIDTH-40, 75);
    addBankBut.layer.masksToBounds = YES;
    addBankBut.layer.cornerRadius = 8;
    [self.tabFootView addSubview: addBankBut];
    [addBankBut addTarget:self action:@selector(addBankButAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(30, 28, 19, 19)];
    [imageV setImage:imgname(@"AddImg")];
    [addBankBut addSubview:imageV];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(imageV.right + 10, 30, 90, 15)];
    titleLab.font = FontSize(15);
    titleLab.text = @"添加银行卡";
    titleLab.textColor = K333333;
    titleLab.textAlignment = NSTextAlignmentCenter;
    [addBankBut addSubview:titleLab];
    
    _mytableView.tableFooterView = self.tabFootView;
}

//获取银行卡列表 数据请求
- (void)getBankNameListData {
    
    [self.mydateSource removeAllObjects];
    [NetWorkTool POST:getFactoryBankCards param:nil success:^(id dic) {
        
        self.mydateSource = [BankCardManageModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.mytableView reloadData];
        
    } other:^(id dic) {
        [self.mytableView reloadData];
    } fail:^(NSError *error) {
        [self.mytableView reloadData];
    } needUser:NO];
}

#pragma mark - 添加银行卡
-(void)addBankButAction:(UIButton *)but{
    //添加银行卡
    AddBankCardViewController *vc = [[AddBankCardViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section, indexPath.row];
    BankCardManageTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    if (!mycell) {
        mycell = [[BankCardManageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    mycell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell的点击状态
    self.mytableView.separatorStyle = UITableViewCellEditingStyleNone;//不显示分割线
    
    //数据赋值
    BankCardManageModel *model = [self.mydateSource safeObjectAtIndex:indexPath.row];
    [mycell refasf:model];
    [mycell.rightTopBtn addTarget:self action:@selector(jieChuBangDingClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return mycell;
}
-(void)jieChuBangDingClick:(UIButton *)button {
    kWeakSelf;
    CommonPopViewController *popVC = [CommonPopViewController new];
    popVC.message = @"是否确定解绑此银行卡？";
    popVC.cancleBtnTitle = @"取消";
    popVC.confirmBtnTitle = @"确定";
    popVC.confirmBtnClick = ^{
        UIView *view = [[button superview] superview];
        BankCardManageTableViewCell *cell = (BankCardManageTableViewCell *)[view superview];
        NSIndexPath *indexPath = [self->_mytableView indexPathForCell:cell];
        BankCardManageModel *model = [BankCardManageModel mj_objectWithKeyValues:[self.mydateSource safeObjectAtIndex:indexPath.row]];
        
        NSDictionary *param = @{
            @"id":NOTNIL(model.id)
        };
        [NetWorkTool POST:delFactoryBankCard param:param success:^(id dic) {
            
            ShowToastWithText(@"解除绑定");
            [weakSelf getBankNameListData];
            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
            
        } fail:^(NSError *error) {
        } needUser:NO];
    };
    [self presentViewController:popVC animated:YES completion:nil];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.selectRowStyle ==1) {
        
        BankCardManageModel *model = [BankCardManageModel mj_objectWithKeyValues:[self.mydateSource safeObjectAtIndex:indexPath.row]];
        if (self.selectedModel) {
            self.selectedModel(model);
        }
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        //设置默认银行卡
        BankCardManageModel *model = [BankCardManageModel mj_objectWithKeyValues:[self.mydateSource safeObjectAtIndex:indexPath.row]];
        
        
        
        NSDictionary *param = @{
            @"id":NOTNIL(model.id)
        };
        [NetWorkTool POST:setFactoryBankDefault param:param success:^(id dic) {
            
            ShowToastWithText(@"设置默认卡成功");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
            
        } fail:^(NSError *error) {
            
        } needUser:NO];
        
    }
    
}

@end
